package com.kehutong.auth.service;

import com.kehutong.auth.entity.Account;
import com.kehutong.auth.entity.AccountOperateLog;
import com.kehutong.auth.enums.OperateType;
import org.coraframework.bean.Beans;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.util.Enums;

@WebService("/auth/accountOperateLog/service")
public class AccountOperateLogService {
    static final Logger logger = LoggerFactory.getLogger(AccountOperateLogService.class);

    @Inject
    private JdbcSession jdbcSession;

    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        return jdbcSession.findArray(AccountOperateLog.class)
                .eq(jsonObject, "id", "accountNo")
                .between("createTime", jsonObject, "createTime", "createTime")
                .eq("deleted", false)
                .exe();
    }

    @ReqMapping("/get")
    public Object get(AccountOperateLog accountOperateLog) {
        return accountOperateLog;
    }

    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(AccountOperateLog accountOperateLog) {
        jdbcSession.insert(accountOperateLog);
        return Result.success();
    }


    @ReqMapping("/add")
    public Object add(JSONObject jsonObject) {
        AccountOperateLog accountOperateLog = Pool.newInstance(AccountOperateLog.class);
        Beans.from(jsonObject).to(accountOperateLog).exclude("id").igornNull().copy();
        OperateType type = Enums.getEnum(OperateType.class, jsonObject.getString("type"));
        accountOperateLog.setType(type);
        jdbcSession.insert(accountOperateLog);
        return Result.success();
    }

    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(AccountOperateLog accountOperateLog) throws Exception {
        jdbcSession.updateById(accountOperateLog);
        return Result.success();
    }

    @ReqMapping("/delete")
    public Object delete(AccountOperateLog accountOperateLog) {
        accountOperateLog.setDeleted(true);
        jdbcSession.updateById(accountOperateLog);
        return Result.success();
    }

}
